package com.project.server.estates;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.SecurityUtils;
import com.project.common.utils.StringUtils;
import com.project.common.utils.poi.ExcelUtil;
import com.project.server.domain.estates.TServiceRecord;
import com.project.server.service.companies.ITCompanyService;
import com.project.server.service.estates.ITServiceRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务记录Controller
 *
 * @author project
 * @date 2021-06-28
 */
@RestController
@RequestMapping("/server/serviceRecord")
public class TServiceRecordController extends BaseController {

    @Resource
    private ITServiceRecordService tServiceRecordService;

    @Resource
    private ITCompanyService companyService;

    /**
     * 查询服务记录列表
     */
    @PreAuthorize("@ss.hasPermi('server:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TServiceRecord tServiceRecord) {
        startPage();
        List<TServiceRecord> list = tServiceRecordService.selectTServiceRecordList(tServiceRecord);
        return getDataTable(list);
    }

    /**
     * 导出服务记录列表
     */
    @PreAuthorize("@ss.hasPermi('server:record:export')")
    @Log(title = "服务记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TServiceRecord tServiceRecord) {
        List<TServiceRecord> list = tServiceRecordService.selectTServiceRecordList(tServiceRecord);
        ExcelUtil<TServiceRecord> util = new ExcelUtil<TServiceRecord>(TServiceRecord.class);
        return util.exportExcel(list, "服务记录列表");
    }

    /**
     * 获取服务记录详细信息
     */
   // @PreAuthorize("@ss.hasPermi('server:record:query')")
    @GetMapping(value = {"/", "/{serviceId}"})
    public AjaxResult getInfo(@PathVariable(value = "serviceId", required = false) Long serviceId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("comps", companyService.selectTCompanyAll());
        if (StringUtils.isNotNull(serviceId)){
            ajax.put(AjaxResult.DATA_TAG,tServiceRecordService.selectTServiceRecordById(serviceId));
            ajax.put("compId", companyService.selectTCompanyByServiceId(serviceId));
        }
        return ajax;
    }

    /**
     * 新增服务记录
     */
    @PreAuthorize("@ss.hasPermi('server:record:add')")
    @Log(title = "服务记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TServiceRecord tServiceRecord) {
        return toAjax(tServiceRecordService.insertTServiceRecord(tServiceRecord));
    }

    /**
     * 修改服务记录
     */
    @PreAuthorize("@ss.hasPermi('server:record:edit')")
    @Log(title = "服务记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TServiceRecord tServiceRecord) {
        tServiceRecord.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tServiceRecordService.updateTServiceRecord(tServiceRecord));
    }

    /**
     * 修改服务记录完成状态
     */
    @PreAuthorize("@ss.hasPermi('server:record:edit')")
    @Log(title = "服务记录", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@Validated @RequestBody TServiceRecord tServiceRecord) {
        tServiceRecord.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tServiceRecordService.changeTServiceRecordStatus(tServiceRecord));
    }

    /**
     * 删除服务记录
     */
    @PreAuthorize("@ss.hasPermi('server:record:remove')")
    @Log(title = "服务记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serviceIds}")
    public AjaxResult remove(@PathVariable Long[] serviceIds) {
        return toAjax(tServiceRecordService.deleteTServiceRecordByIds(serviceIds));
    }
}
